import { login } from "../../utils/asyncWx.js";
import regeneratorRuntime from "../../lib/runtime/runtime";
import { request } from "../../request/index";
Page({
  data: {},
  // 获取用户信息
  async handleGetUserInfo(e) {
    // console.log(e);
    try {
      // 获取用户信息 encryptedData,errMsg,iv,signature
      const { encryptedData, errMsg, iv, signature } = e.detail;
      // 获取小程序登录成功后的code
      const { code } = await login();
      const loginParams = { encryptedData, errMsg, iv, signature, code };
      // console.log(code);
      // 发送请求 获取用户的token
      const { token } = await request({
        url: "/users/wxlogin",
        data: loginParams,
        method: "post",
      });
      // console.log(res);
      // 把token存入缓存中 同时跳转回上一个页面
      wx.setStorageSync("token", token);
      // 1表示返回上一层 2就返回上两层
      wx.navigateBack({
        delta: 1,
      });
    } catch (error) {
      console.log(error);
    }
  },
});
